package structure;

public class DoubleLinkedList1 {
    private static class Node {
        int val;
        Node next;
        Node prev;

        Node(int val) {
            this.val = val;
        }
    }

    private Node head; // 头指针
    private Node last; // 尾指针

    public void addFirst(int data) {
        Node node = new Node(data);
        if (head == null) {
            head = node;
            last = node;
            return;
        }
        node.next = head;
        head.prev = node;
        head = node;
    }

    public void addLast(int data) {
        Node node = new Node(data);
        if (head == null) {
            head = last = node;
            return;
        }
        last.next = node;
        node.prev = last;
        last = node;
    }

    public void addIndex(int data, int index) {
        if (index < 0 || index > size()) {
            return;
        }
        if (index == 0) {
            addFirst(data);
            return;
        }
        if (index == size()) {
            addLast(data);
            return;
        }

        Node cur = head;
        while (index != 0) {
            cur = cur.next;
            index--;
        }
        Node prev = cur.prev;
        Node node = new Node(data);
        node.next = cur;
        cur.prev = node;

        prev.next = node;
        node.prev = prev;
    }

    public int size() {
        Node cur = head;
        int size = 0;
        while (cur != null) {
            size++;
            cur = cur.next;
        }
        return size;
    }

    public void print() {
        Node cur = head;
        while (cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    public boolean contains(int data) {
        Node cur = head;
        while (cur != null) {
            if (cur.val == data) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    public void remove(int data) {
        if (head == null) {
            return;
        }
        Node cur = head;
        while (cur != null) {
            if (cur.val == data) {
                if (cur == head) {
                    head = head.next;
                    if (head == null) {
                        // 链表只有一个节点的情况
                        last = null;
                    }else {
                        // 如果存在两个或多个节点, 那么就把其下一个节点的 prev 指针置空
                        head.prev = null;
                    }
                }else if (cur == last){
                    // 如果当前节点cur是尾节点
                    last = cur.prev;
                    last.next = null;
                }else {
                    // 如果要删除的节点在中间
                    cur.prev.next = cur.next;
                    cur.next.prev = cur.prev;
                }
                return;
            }
            cur = cur.next;
        }
    }

    public void removeAll(int data) {
        if (head == null) {
            return;
        }
        Node cur = head;
        while (cur != null) {
            if (cur.val == data) {
                if (cur == head) {
                    head = head.next;
                    if (head == null) {
                        // 链表只有一个节点的情况
                        last = null;
                    }else {
                        // 如果存在两个或多个节点, 那么就把其下一个节点的 prev 指针置空
                        head.prev = null;
                    }
                }else if (cur == last){
                    // 如果当前节点cur是尾节点
                    last = cur.prev;
                    last.next = null;
                }else {
                    // 如果要删除的节点在中间
                    cur.prev.next = cur.next;
                    cur.next.prev = cur.prev;
                }
            }
            cur = cur.next;
        }
    }

    public static void main(String[] args) {
        DoubleLinkedList1 linkedList1 = new DoubleLinkedList1();
        linkedList1.addLast(3);
        linkedList1.addLast(3);
        linkedList1.addLast(3);
        linkedList1.addLast(3);
        linkedList1.addLast(1);
        linkedList1.print();

        linkedList1.removeAll(3);
        linkedList1.print();
    }

    public void clear() {
        Node cur = head;
        while (cur != null) {
            Node curNext = cur.next;
            cur.prev = null;
            cur.next = null;
            cur = curNext;
        }
        head = last = null;
    }
}
